<template>
	<view class="container">
		<up-cell-group>
			<up-cell title="用户名" :value="'@'+userInfo.username"></up-cell>
			<up-cell title="昵称" :value="userInfo.nickname"></up-cell>
			<up-cell title="生日" :value="userInfo.nickname"></up-cell>
			<up-cell title="昵称" :value="userInfo.nickname"></up-cell>
			<up-cell title="会员等级" value="新版本"></up-cell>
			<up-cell title="位置" :value="address || '未知'" @click="chooseLocation"></up-cell>
		</up-cell-group>
	</view>
</template>

<script setup>
	import {
		ref,
		shallowRef
	} from 'vue'
	import {
		useUserStore
	} from '/stores/user.js'
	import {
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	import http from '/utils/request.js'

	const locationInfo = shallowRef({
		longitude: 0,
		latitude: 0
	})

	const address = ref('')

	const userStore = useUserStore()

	const userInfo = shallowRef({
		avatar: '',
		birthday: '无',
		email: '无',
		nickname: '无',
		qq: '无',
		sex: 1,
		username: '无',
		wx: '无'
	})

	const getUserInfo = () => {
		userInfo.value = {
			...userStore.userInfo
		}
	}

	function chooseLocation() {
		uni.showLoading({
			title: '获取定位中...',
			icon: 'info'
		})
		uni.getLocation({
			type: 'gcj02',
			provider: 'android',
			success: res => {
				console.log(res);
				locationInfo.longitude = res.longitude
				locationInfo.latitude = res.latitude

				const key = '757a190c6e2e005e37e7d4a8d1de879e'
				const location = `${locationInfo.longitude},${locationInfo.latitude}`
				const r = uni.request({
					url: 'https://restapi.amap.com/v3/geocode/regeo',
					data: {
						key,
						location
					},
					method: 'GET',
					success: res => {
						uni.hideLoading()
						console.log(res)
						address.value = res.data.regeocode.formatted_address
					}
				})
			},
			fail: err => {
				console.error(err);
			}
		})
	}


	onPullDownRefresh(() => {
		if (userStore.getUserInfo()) {
			uni.stopPullDownRefresh()
			uni.showToast({
				title: '刷新成功',
				icon: 'success'
			})
		}
	})
</script>

<style lang="scss">
	:deep(.u-cell__value) {
		max-width: 70%;
	}
</style>